package cn.kgc.ssm.aspects;

import cn.kgc.ssm.annotations.LogOperation;
import cn.kgc.ssm.entity.LogInfo;
import cn.kgc.ssm.entity.User;
import cn.kgc.ssm.service.LogInfoService;
import cn.kgc.ssm.util.IpUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @author 课工场
 * @date 2023/10/28
 * @description 操作日志通知类
 */
@Aspect
@Component
public class LogAspect {

    @Resource
    private LogInfoService logInfoService;

    @Pointcut("@annotation(cn.kgc.ssm.annotations.LogOperation)")
    public void pointCut() {} ;


    @Around("pointCut()")
    public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) {

        Object proceed = null;
        try {
            proceed = proceedingJoinPoint.proceed();
            // username   ip  params   url  description   model

            //  1.params   请求传递的参数   [1,2,3,4]  =>  1,2,3,4
            Object[] args = proceedingJoinPoint.getArgs();
            String strArgs = StringUtils.arrayToCommaDelimitedString(args);

            // 2. description   model   Method

            MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
            Method method = signature.getMethod();

            LogOperation annotation = method.getAnnotation(LogOperation.class);
            String model = annotation.model();
            String description = annotation.description().getDescription();


            // 3. username  ip  uri    ->  request
            ServletRequestAttributes  requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = requestAttributes.getRequest();

            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("user");
            String username = user.getUsername();

            String uri = request.getRequestURI();
            String ip = IpUtil.getIp(request);

            // 4.封装日志数据
            LogInfo logInfo = new LogInfo();
            logInfo.setUsername(username);
            logInfo.setIp(ip);
            logInfo.setModel(model);
            logInfo.setUrl(uri);
            logInfo.setDescription(description);
            logInfo.setParams(strArgs);
            logInfo.setCreateTime(new Date());

            // 5. 将操作日志写入数据库
            logInfoService.addInfo(logInfo);

        } catch (Throwable e) {
            throw new RuntimeException(e);
        }


        return proceed;

    }

}
